package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.FenceStoreStockBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.FenceStoreStockMapper;
import com.molichuxing.services.infrastructure.dto.request.create.FenceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.FenceModifyDto;
import com.molichuxing.services.infrastructure.dto.response.FenceCountDto;
import com.molichuxing.services.infrastructure.dto.response.FenceStoreStockDto;
import com.molichuxing.services.infrastructure.service.FenceStoreStockService;
import com.molichuxing.services.property.FenceStoreStockTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 门店中转库围栏绑定 服务类
 * </p>
 *
 * @author
 * @since 2020-02-20
 */
@Service("fenceStoreStockService")
public class FenceStoreStockServiceImpl implements FenceStoreStockService {

    @Autowired
    FenceStoreStockMapper fenceStoreStockMapper;

    /**
     * 获取门店仓库围栏数量
     *
     * @param storeIds
     * @param type
     * @return
     */
    @Override
    public Map<Integer, Integer> getFenceCount(List<Integer> storeIds, Integer type) {
        if (null == storeIds || storeIds.size() == 0) {
            return null;
        }

        List<FenceCountDto> fenceCountList = fenceStoreStockMapper.getFenceCount(storeIds, type);
        if (null == fenceCountList || fenceCountList.size() == 0) {
            return null;
        }

        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        for (FenceCountDto fenceCountDto : fenceCountList) {
            Integer storeId = fenceCountDto.getStoreId();
            Integer count = fenceCountDto.getCount();
            returnMap.put(storeId, count);
        }

        return returnMap;
    }

    /**
     * 围栏添加
     *
     * @param fenceCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean createFence(FenceCreateDto fenceCreateDto) throws Exception {
        Assert.notNull(fenceCreateDto, "围栏添加失败");
        Integer storeId = fenceCreateDto.getStoreId();
        Integer type = fenceCreateDto.getType();
        List<Integer> fenceIdList = fenceCreateDto.getFenceIdList();
        if (null == fenceIdList || fenceIdList.size() < 1) {
            throw new BizException("围栏添加失败");
        }

        List<Integer> existFenceIdList = fenceStoreStockMapper.getFenceByStoreId(storeId, type);
        List<FenceStoreStockBean> addList = new ArrayList<>();
        for (Integer fenceId : fenceIdList) {
            if (existFenceIdList != null && getAllFenceIdList().size() > 0 && existFenceIdList.contains(fenceId)) {
                continue;
            }

            FenceStoreStockBean fenceStoreStockBean = new FenceStoreStockBean();
            fenceStoreStockBean.setFenceId(fenceId);
            fenceStoreStockBean.setStoreId(storeId);
            fenceStoreStockBean.setType(type);
            addList.add(fenceStoreStockBean);
        }

        int fenceBatchCount = fenceStoreStockMapper.createFenceBatch(addList);
        if (fenceBatchCount == addList.size()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过门店或中转库id获取围栏id
     *
     * @param storeId
     * @param fenceStoreStockType
     * @return
     */
    @Override
    public List<Integer> getFenceByStoreId(Integer storeId, FenceStoreStockTypeEnum fenceStoreStockType) {
        if (null == fenceStoreStockType) {
            return null;
        }
        return fenceStoreStockMapper.getFenceByStoreId(storeId, fenceStoreStockType.getValue());
    }

    /**
     * 围栏解除
     *
     * @param fenceModifyDto
     * @return
     */
    @Override
    public Boolean deleteFence(FenceModifyDto fenceModifyDto) {
        Integer storeId = fenceModifyDto.getStoreId();
        Integer type = fenceModifyDto.getType();
        Integer fenceId = fenceModifyDto.getFenceId();

        int result = fenceStoreStockMapper.deleteFence(storeId, type, fenceId);
        if (result > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据围栏id获取围栏信息
     *
     * @param fenceIdList
     * @return
     */
    @Override
    public List<FenceStoreStockDto> getFenceByFenceIdList(List<Integer> fenceIdList) {
        List<FenceStoreStockBean> fenceBeanList = fenceStoreStockMapper.getFenceByFenceIdList(fenceIdList);
        return Convert.toFenceStoreStockDtoList(fenceBeanList);
    }

    /**
     * 获取所有围栏id
     *
     * @return
     */
    @Override
    public List<Integer> getAllFenceIdList() {
        return fenceStoreStockMapper.getAllFenceIdList();
    }

    @Override
    public List<Integer> getFenceIdByStoreIds(List<Integer> storeIds, Integer type) {
        return fenceStoreStockMapper.getFenceIdByStoreIds(storeIds,type);
    }

    @Override
    public Map<Integer, Integer> getStoreIdByFenceIds(List<Integer> fenceIds, Integer type) {
        Map<Integer, Integer>map=new HashMap<>();
        List<FenceStoreStockBean> storeIdByFenceIds = fenceStoreStockMapper.getStoreIdByFenceIds(fenceIds, type);
           if (CollectionUtils.isEmpty(storeIdByFenceIds)){
               return map;
           }
        for (FenceStoreStockBean store : storeIdByFenceIds) {
            map.put(store.getFenceId(),store.getStoreId());
        }
        return map;
    }


}
